package org.jsping.filter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.jsping.music.MusicCache;

/**
 * 音乐监听器
 */
@WebListener
public class MusicListener implements ServletContextListener{

	/**
	 * 在服务启动的时候初始化该任务
	 * 定时去拿取36首音乐,如果服务器不关闭的话,是1天1拿
	 * new MusicCache() 是一个线程任务,继承了runnable接口
	 */
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("初始化Music监听器(定时任务)");
		ScheduledExecutorService music = Executors.newScheduledThreadPool(1);
		music.scheduleAtFixedRate(new MusicCache(), 0, 1,TimeUnit.DAYS);
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("Music监听器被销毁");
	}
}
